package com.originskyseed.platform.course.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.course.mapper.CourseTypeMapper;
import com.originskyseed.platform.course.model.CourseType;
import com.originskyseed.platform.course.service.ICourseTypeService;

/**
 * 课程类型表Service接口实现类（OSI_COURSE_TYPE）
 * 
 * @author Tangwanghui
 */
@DubboService
@Component("courseTypeService")
public class CourseTypeServiceImpl implements ICourseTypeService {

	@Resource
	private CourseTypeMapper courseTypeMapper;

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.courseTypeMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.courseTypeMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 批量更新删除状态
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	public int updateDelByIds(List<String> ids) throws Exception {
		return this.courseTypeMapper.updateDelByIds(ids);
	}

	/**
	 * 更新置顶信息
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateIsTop(CourseType record) throws Exception {
		record.setUpdateTime(new Date());
		this.courseTypeMapper.updateIsTop(record);
		return this.courseTypeMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(CourseType record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.courseTypeMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(CourseType record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.courseTypeMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(CourseType record) throws Exception {
		record.setUpdateTime(new Date());
		return this.courseTypeMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(CourseType record) throws Exception {
		record.setUpdateTime(new Date());
		return this.courseTypeMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public CourseType selectByPrimaryKey(String id) throws Exception {
		return this.courseTypeMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CourseType> selectList(CourseType record) throws Exception {
		return this.courseTypeMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<CourseType> selectListByPage(Map<String, Object> params, BasePage<CourseType> page) throws Exception {
		params.put("page", page);
		List<CourseType> list = this.courseTypeMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 查询课程类型id是否存在
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public String selectCourseTypeId(String id) throws Exception {
		return this.courseTypeMapper.selectCourseTypeId(id);
	}

	/**
	 * 查询最大的优先级
	 * 
	 * @return
	 * @throws Exception
	 */
	public Integer selectPrority() throws Exception {
		return this.courseTypeMapper.selectPrority();
	}

	/**
	 * 更新排序信息
	 *
	 * @param downCourse
	 * @param upCourse
	 * @return
	 * @throws Exception
	 */
	public int updatePrority(CourseType downCourse, CourseType upCourse) throws Exception {
		downCourse.setUpdateTime(new Date());
		upCourse.setUpdateTime(new Date());
		this.courseTypeMapper.updateByPrimaryKeySelective(downCourse);
		return this.courseTypeMapper.updateByPrimaryKeySelective(upCourse);
	}

	/**
	 * 查询优先级
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public CourseType selectCourseTypeByPriority(Map<String, Object> map) throws Exception {
		return this.courseTypeMapper.selectCourseTypeByPriority(map);
	}

}